package com.bml.www.teacher.common.http;

import android.text.TextUtils;

import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import com.bml.www.teacher.app.MyApplication;

/**
 * Token info
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/7/15 16:17
 * @Version: 1.0.0
 */
public class TokenInfo {
    /**
     * access_token : 8F00B204E9800998B9C351AB2848D4005F3F355BA3C214DD9728ED1EC877D107A6348DBB728D4388
     * refresh_token : 8F00B204E98009980E30A329013B0201CCB05B8F59699C554FE3BBF343585632997F2BB15D38FBC6
     * expires_at : 2016-07-28T13:54:53.543+0800
     * server_time : 2016-07-14T13:54:53.543+0800
     * mac_key : cb3nZO8bjD
     */


    private static final String SP_FILENAME_TOKEN_INFO = TokenInfo.class.getSimpleName();
    private static final String KEY_ACCESS_TOKEN = "access_token";
    private static final String KEY_REFRESH_TOKEN = "refresh_token";
    private static final String KEY_EXPIRES_AT = "expires_at";
    private static final String KEY_SERVER_TIME = "server_time";
    private static final String KEY_MAC_KEY = "mac_key";
    private static final String KEY_USER_ID = "user_id";

    private String access_token;
    private String refresh_token;
    private String expires_at;
    private String server_time;
    private String mac_key;
    private String user_id;


    public static TokenInfo createFromSp() {
        TokenInfo tokenInfo = new TokenInfo();
        tokenInfo.setAccessToken(getString(KEY_ACCESS_TOKEN));
        tokenInfo.setRefreshToken(getString(KEY_REFRESH_TOKEN));
        tokenInfo.setExpiresAt(getString(KEY_EXPIRES_AT));
        tokenInfo.setMacKey(getString(KEY_MAC_KEY));
        tokenInfo.setServerTime(getString(KEY_SERVER_TIME));
        tokenInfo.setUser_id(getString(KEY_USER_ID));
        return tokenInfo;
    }

    public String getMacKey() {
        return mac_key;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param macKey
     */
    public void setMacKey(String macKey) {
        if (!TextUtils.equals(this.mac_key, macKey)) {
            this.mac_key = macKey;
            saveString(KEY_MAC_KEY, mac_key);
        }
    }


    public String getServerTime() {
        return server_time;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param serverTime
     */
    public void setServerTime(String serverTime) {
        if (!TextUtils.equals(this.server_time, serverTime)) {
            this.server_time = serverTime;
            saveString(KEY_SERVER_TIME, server_time);
        }
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        if (!TextUtils.equals(this.user_id, user_id)) {
            this.user_id = user_id;
            saveString(KEY_USER_ID,user_id);
        }
    }


    public String getExpiresAt() {
        return expires_at;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param expiresAt
     */
    public void setExpiresAt(String expiresAt) {
        if (!TextUtils.equals(this.expires_at, expiresAt)) {
            this.expires_at = expiresAt;
            saveString(KEY_EXPIRES_AT, expires_at);
        }
    }


    public String getRefreshToken() {
        return refresh_token;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param token refresh token
     */
    public void setRefreshToken(String token) {
        if (!TextUtils.equals(this.refresh_token, token)) {
            this.refresh_token = token;
            saveString(KEY_REFRESH_TOKEN, refresh_token);
        }
    }


    public String getAccessToken() {
        return access_token;
    }

    /**
     * 无需判断name为空的情况， 用户需要通过此api清除该属性的值
     *
     * @param token access token
     */
    public void setAccessToken(String token) {
        if (!TextUtils.equals(this.access_token, token)) {
            this.access_token = token;
            saveString(KEY_ACCESS_TOKEN, access_token);
        }
    }

    public void clear() {
        setAccessToken("");
        setRefreshToken("");
        setExpiresAt("");
        setMacKey("");
        setServerTime("");
        setUser_id("");
    }


    @Override
    public String toString() {
        return "TokenInfo{" +
                "access_token='" + getAccessToken() + '\'' +
                ", refresh_token='" + getRefreshToken() + '\'' +
                ", expires_at='" + getExpiresAt() + '\'' +
                ", server_time='" + getServerTime() + '\'' +
                ", mac_key='" + getMacKey() + '\'' +
                '}';
    }

    public String getMacAlgorithm() {
        return "HmacSHA256";
    }


    private static void saveString(String key, String value) {
        SharePreferenceUtil.saveString(MyApplication.getAppContext(), SP_FILENAME_TOKEN_INFO, key, value);
    }

    private static String getString(String key) {
        return SharePreferenceUtil.getString(MyApplication.getAppContext(), SP_FILENAME_TOKEN_INFO, key);
    }

}

